function getPagenation(data){
	var div = $(data).find("#goods_list");
	$("#goods_list").html($(div[0]).html());
}

function deleteC(data,domId){
	$("#"+domId).remove();
	var inputId = domId.replace("_a","");
	$("#"+inputId).val("");
	var url = appPath+"/goods/list.htm";
	params = $("#query_form").serialize();
	var searchUrl = url+"?"+params;
	self.location=searchUrl;
	//$("#jump_link").attr("href",searchUrl);
	//$("#jump_link").click;
}

$(document).ready(function(){
	var sortParam = $("#sort_input").val();
	if(sortParam){
		var tem = sortParam.split("_");
		$(".sort_a[col='"+tem[0]+"']").attr("orderType",tem[1]);
		$(".sort_a").removeClass("curr");
		$(".sort_a[col='"+tem[0]+"']").addClass("curr");
		if("price"==tem[0]){//只有价格正反序
			$(".sort_a i").addClass(tem[1]);
		}
	}
	$("#page_div").myPagination({
		currPage : 1,
		pageCount : $("#pageCount").val(),
		pageSize : 10,
		ajax : {
			on : true,
			callback : 'getPagenation',
			url : appPath+'/goods/findPageResult.htm?'+ $("#query_form").serialize(),
			data:{
				page:$("#currentPage").val()
			},
			dataType : 'html'
		}
	});
	//显示已选择的条件
	var goodsClassId = $("#goodsClassId").val();
	var brandId = $("#brandId").val();
	var attrIds = $("input[id^='attrId']");
	var attrValue = $("#attrValue").val();
	var keyWord = $("#keyWord").val();
	var condition="";
	if(goodsClassId){
		 var selGoodsClass = $("#goodsClass_dl").find("a[dataValue='"+goodsClassId+"']");
		 condition += '<a class="inbtn pzbtn" id="goodsClassId_a" onclick=deleteC("'+goodsClassId+'","goodsClassId_a")><span >'+$("#goodsClassName").val()+'</span></a>';
		 selGoodsClass.addClass("seled");
	}
	if(goodsClassId&&brandId){
		var selBrand = $("#brand_dl").find("a[dataValue='"+brandId+"']");
		condition += '<a class="inbtn pzbtn" id="brandId_a" onclick=deleteC("'+brandId+'","brandId_a") ><span >'+$("#brandName").val()+'</span></a>';
		selBrand.addClass("seled");
	}
	if(goodsClassId&&attrIds&&attrIds.length>0){
		for(var i =0;i<attrIds.length;i++){
			if($(attrIds[i]).val()){
				var selAttr = $(".attrClass_dl").find("a[dataValue='"+$(attrIds[i]).val()+"']");
				condition += '<a id="" class="inbtn pzbtn" id="attrId_a" onclick=deleteC("'+$(attrIds[i]).val()+'","attrId'+i+'_a") ><span >'+selAttr.html()+'</span></a>';
				selAttr.addClass("seled");
			}
		}
	}
	if(keyWord&&keyWord.replace(/^\s*|\s*$/g,'')){
		condition += '<a class="keyword_span"><span >"'+keyWord+'"</span></a>';
		//搜索框回写
		//$("#searchinput").val(keyWord);
	}
	$("#condition").append(condition);
	
	$(".sort_a").click(function(){
		var col = $(this).attr("col");
		var orderType = $(this).attr("orderType");
		if("price"==col){//只有价格正反序
			orderType = orderType == "asc" ?"desc":"asc";
		}
		var sort = col+"_"+orderType;
		$("#sort_input").val(sort);
		//var sortUrl = url+"/0/"+sort+".html";
		var url = appPath+"/goods/list.htm";
		params = $("#query_form").serialize();
		var sortUrl = url+"?"+params;
		$(this).attr("href",sortUrl);
	});
	var goodsClassId=$("#goodsClassId").val();
	$(".filter_a").click(function(){
		var inputId = $(this).attr("inputid");
		var dataValue = $(this).attr("datavalue");
		if(inputId=="goodsClassId"){//如果是类别修改
			var preGoodsClassId = $("#preGoodsClassId").val();
			if(preGoodsClassId!=dataValue){//清空
				$(":hidden[id^='attrId']").val("");
				$("#brandId").val("");
			}
			$("#goodsClassName").val($(this).html());
		}
		var temFlag = true;
		if(inputId.match(/^attrId\d+$/g)!=null){//attr
			if(document.getElementById(inputId) == null){//没有
				temFlag = false;
				var index = inputId.replace("attrId","");
				var dom = '<input type="hidden" id="attrId'+index+'" name="attrIds['+index+']" value="'+dataValue+'" />'
				$("#query_form").append(dom);
			}
		}
		if(temFlag){
			$("#"+inputId).val(dataValue);
		}
		var url = appPath+"/goods/list.htm";
		params = $("#query_form").serialize();
		var searchUrl = url+"?"+params;
		$(this).attr("href",searchUrl);
	});
});